package com.ljh.server.handler;

import com.ljh.message.GroupQuitRequestMessage;
import com.ljh.message.GroupQuitResponseMessage;
import com.ljh.server.session.Group;
import com.ljh.server.session.GroupSessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @author: Themberfue
 * @date: 2025/5/29 22:48
 * @description:
 */
@ChannelHandler.Sharable
public class GroupQuitRequestHandler extends SimpleChannelInboundHandler<GroupQuitRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupQuitRequestMessage msg) throws Exception {
        String groupName = msg.getGroupName();
        String username = msg.getUsername();

        Group group = GroupSessionFactory.getGroupSession().removeMember(groupName, username);
        if (group == null) {
            ctx.writeAndFlush(new GroupQuitResponseMessage(false, "群聊不存在"));
        } else {
            ctx.writeAndFlush(new GroupQuitResponseMessage(true, "退群成功"));
        }
    }
}
